<?
// CHECK DEATHS
if ($_SESSION['jog1']->getValue("hp") <= -10
	|| $_SESSION['jog1']->getValue("nonLethalDamage") > $_SESSION['jog1']->getValue("hp")) {
	$_SESSION['jog2_vit']++; $continue = false;
}
if ($_SESSION['jog2']->getValue("hp") <= -10
	|| $_SESSION['jog2']->getValue("nonLethalDamage") > $_SESSION['jog2']->getValue("hp")) {
	$_SESSION['jog1_vit']++;  $continue = false;
}

//START OF ROUND
$_SESSION['rounds']++;

// GET ATTACKER MODIFIERS
$att_size_mod = floor((6 - $atacante->getValue('size')) * 1.2);
$att_str_mod = getMod($atacante->getValue('str'));
$att_dex_mod = getMod($atacante->getValue('dex'));
$att_con_mod = getMod($atacante->getValue('con'));
$att_int_mod = getMod($atacante->getValue('int'));
$att_wis_mod = getMod($atacante->getValue('wis'));
$att_car_mod = getMod($atacante->getValue('car'));

// GET DEFENDER MODIFIERS
$def_size_mod = floor((6 - $defensor->getValue('size')) * 1.2);
$def_str_mod = getMod($defensor->getValue('str'));
$def_dex_mod = getMod($defensor->getValue('dex'));
$def_con_mod = getMod($defensor->getValue('con'));
$def_int_mod = getMod($defensor->getValue('int'));
$def_wis_mod = getMod($defensor->getValue('wis'));
$def_car_mod = getMod($defensor->getValue('car'));

$melee_attack_mod = 0; $ranged_attack_mod = 0; $armor_on_melee_mod = 0; $armor_on_ranged_mod = 0;
$ranged_attack_allowed = true; $rogue_sneak_attack = false; $attack_autocritical = false;
include('conditions_modifiers.php');

// ATTACK ROLL
$dado = new Dado(20);
$attack_roll = $dado->lancar(1, 0);

// ARMOR CLASS
$armor_class = 10 + $defensor->getValue('naturalArmor') + $armor_bonus + $shield_bonus + $def_dex_mod + 	$def_size_mod;
	
$action = 'attack_melee';
if ($action == 'attack_ranged' && !$ranged_attack_allowed) $action = 'failed';

if ($action!='failed') {
	if ($attack_roll > 1) {
		if ($action == 'attack_melee') {
			$armor_class += $armor_on_melee_mod;
			$attack_roll += $melee_attack_mod;
			$attack_bonus = $atacante->getValue('baseAttack') + $att_str_mod + $att_size_mod;
		}
		if ($action == 'attack_ranged') {
			$armor_class += $armor_on_ranged_mod;
			$attack_roll += $ranged_attack_mod;
			$attack_bonus = $atacante->getValue('baseAttack') + $att_dex_mod + $att_size_mod;
		}
	} else $attack_bonus = 0;
	
	// GET THE RESULT!
	$attack_roll += $attack_bonus;
	$resultado = $attack_roll - $armor_class;
	
	if ($resultado > 0) {
		// DAMAGE ROLL
		$damage_roll = $atacante->getArma()->damage();
		if ($action == 'attack_melee')
			$dano_final = $damage_roll + $att_str_mod;
		
		// CRITICAL HIT
		$critical = $atacante->getArma()->getValue('critical');
		if ($damage_roll >= 20 - $critical[1]) $dano_final *= $critical[0];
		
		// DAMAGE IS AT LEAST 1
		if ($dano_final <= 0) $dano_final = 1;
			
		// ACTUALIZAR HP DO DEFENSOR
		if ($atacante->getArma()->getValue('nonLethalDamage'))
			$defensor->setValue("nonLethalDamage", $defensor->getValue("nonLethalDamage") + $dano_final);
		else $defensor->setValue("hp", $defensor->getValue("hp") - $dano_final);
	}
	
} else {
	$attack_result = 'falhou!';
}
?>